package com.ruoyi.combat.service.camera.impl;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

import com.ruoyi.combat.domain.camera.CombatCameraGeo;
import com.ruoyi.combat.domain.camera.CombatCameraGeoResult;
import com.ruoyi.combat.domain.camera.CombatCameraInfo;
import com.ruoyi.combat.domain.vo.Category;
import com.ruoyi.combat.domain.vo.Link;
import com.ruoyi.combat.domain.vo.Node;
import com.ruoyi.combat.mapper.camera.CombatCameraGeoMapper;
import com.ruoyi.combat.mapper.camera.CombatCameraGeoResultMapper;
import com.ruoyi.combat.mapper.camera.CombatCameraInfoMapper;
import com.ruoyi.combat.service.camera.ICombatCameraInfoService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 摄像头信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-08
 */
@Service
public class CombatCameraInfoServiceImpl implements ICombatCameraInfoService {

    private static final Logger log = LoggerFactory.getLogger(CombatCameraInfoServiceImpl.class);
    @Autowired
    private CombatCameraInfoMapper combatCameraInfoMapper;

    @Autowired
    private CombatCameraGeoMapper combatCameraGeoMapper;

    @Autowired
    private CombatCameraGeoResultMapper combatCameraGeoResultMapper;

    /**
     * 查询摄像头信息
     *
     * @param id 摄像头信息主键
     * @return 摄像头信息
     */
    @Override
    public CombatCameraInfo selectCombatCameraInfoById(Long id) {
        return combatCameraInfoMapper.selectCombatCameraInfoById(id);
    }

    /**
     * 查询摄像头信息列表
     *
     * @param combatCameraInfo 摄像头信息
     * @return 摄像头信息
     */
    @Override
    public List<CombatCameraInfo> selectCombatCameraInfoList(CombatCameraInfo combatCameraInfo) {
        return combatCameraInfoMapper.selectCombatCameraInfoList(combatCameraInfo);
    }

    /**
     * 新增摄像头信息
     *
     * @param combatCameraInfo 摄像头信息
     * @return 结果
     */
    @Override
    public int insertCombatCameraInfo(CombatCameraInfo combatCameraInfo) {
        return combatCameraInfoMapper.insertCombatCameraInfo(combatCameraInfo);
    }

    /**
     * 修改摄像头信息
     *
     * @param combatCameraInfo 摄像头信息
     * @return 结果
     */
    @Override
    public int updateCombatCameraInfo(CombatCameraInfo combatCameraInfo) {
        return combatCameraInfoMapper.updateCombatCameraInfo(combatCameraInfo);
    }

    /**
     * 批量删除摄像头信息
     *
     * @param ids 需要删除的摄像头信息主键
     * @return 结果
     */
    @Override
    public int deleteCombatCameraInfoByIds(Long[] ids) {
        return combatCameraInfoMapper.deleteCombatCameraInfoByIds(ids);
    }

    /**
     * 删除摄像头信息信息
     *
     * @param id 摄像头信息主键
     * @return 结果
     */
    @Override
    public int deleteCombatCameraInfoById(Long id) {
        return combatCameraInfoMapper.deleteCombatCameraInfoById(id);
    }

    @Override
    public Map getGraphByCameraInfo(CombatCameraInfo info) {
        List<CombatCameraInfo> list = combatCameraInfoMapper.selectCombatCameraInfoList(info);
        Map<String, Object> result = new HashMap<>();
        List<Node> nodes = new ArrayList<>();
        List<Link> links = new ArrayList<>();
        Set<String> nodeSet = new HashSet<>();
        for (CombatCameraInfo cameraInfo : list) {
            String id = String.valueOf(cameraInfo.getId());
            nodes.add(new Node(id, "摄像头ip", 0, cameraInfo.getCameraIp()));
            nodeSet.add(id);

            if (StringUtils.isNotEmpty(cameraInfo.getCameraType())) {
                String type = cameraInfo.getCameraType();
                String typeid = StringtoMD5(type);
                if (!nodeSet.contains(typeid)) {
                    nodes.add(new Node(typeid, "类型", 1, type));
                    nodeSet.add(typeid);
                }
                links.add(new Link(id, typeid));
            }
            if (StringUtils.isNotEmpty(cameraInfo.getCameraBrand())) {
                String brand = cameraInfo.getCameraBrand();
                String brandid = StringtoMD5(brand);
                if (!nodeSet.contains(brandid)) {
                    nodes.add(new Node(brandid, "品牌", 2, brand));
                    nodeSet.add(brandid);
                }
                links.add(new Link(id, brandid));
            }
            if (StringUtils.isNotEmpty(cameraInfo.getCameraFraPoint())) {
                String point = cameraInfo.getCameraFraPoint();
                String pointid = StringtoMD5(point);
                if (!nodeSet.contains(pointid)) {
                    nodes.add(new Node(pointid, "端口", 3, point));
                    nodeSet.add(pointid);
                }
                links.add(new Link(id, pointid));
            }
            if (StringUtils.isNotEmpty(cameraInfo.getCameraPort())) {
                String port = cameraInfo.getCameraPort();
                String portid = StringtoMD5(port);
                if (!nodeSet.contains(portid)) {
                    nodes.add(new Node(portid, "脆弱点", 4, port));
                    nodeSet.add(portid);
                }
                links.add(new Link(id, portid));
            }
            if (StringUtils.isNotEmpty(cameraInfo.getCameraLoophole())) {
                String hole = cameraInfo.getCameraLoophole();
                String holeid = StringtoMD5(hole);
                if (!nodeSet.contains(holeid)) {
                    nodes.add(new Node(holeid, "漏洞类型", 5, hole));
                    nodeSet.add(holeid);
                }
                links.add(new Link(id, holeid));
            }
            if (StringUtils.isNotEmpty(cameraInfo.getCameraEffect())) {
                String effect = cameraInfo.getCameraEffect();
                String effectid = StringtoMD5(effect);
                if (!nodeSet.contains(effectid)) {
                    nodes.add(new Node(effectid, "影响范围", 6, effect));
                    nodeSet.add(effectid);
                }
                links.add(new Link(id, effectid));
            }
            if (StringUtils.isNotEmpty(cameraInfo.getCameraLevel())) {
                String level = cameraInfo.getCameraLevel();
                String levelid = StringtoMD5(level);
                if (!nodeSet.contains(levelid)) {
                    nodes.add(new Node(levelid, "危害等级", 7, level));
                    nodeSet.add(levelid);
                }
                links.add(new Link(id, levelid));
            }
            if (StringUtils.isNotEmpty(cameraInfo.getCameraMethod())) {
                String method = cameraInfo.getCameraMethod();
                String methodid = StringtoMD5(method);
                if (!nodeSet.contains(methodid)) {
                    nodes.add(new Node(methodid, "利用方法", 8, method));
                    nodeSet.add(methodid);
                }
                links.add(new Link(id, methodid));
            }
        }
        result.put("nodes", nodes);
        result.put("links", links);
        result.put("categories", getCategory());
        return result;
    }
    //重保用户

    @Override
    public Map getGraphByCameraInfoNew(CombatCameraInfo info) {


        Map<String, String> ma = new HashMap<>();
        if (info != null) {
            if (StringUtils.isNotEmpty(info.getCameraIp())) {
                ma.put("cameraIp", info.getCameraIp());
            }
            if (StringUtils.isNotEmpty(info.getCameraArea())) {
                ma.put("cameraArea", info.getCameraArea());
            }
            List<Map> selectCheck = info.getSelectCheck();
            if(selectCheck!=null){
                return doBuild(selectCheck, ma);
            }else{
                return getGraphByCameraInfoNew2(info);
            }
        }
        return getGraphByCameraInfoNew2(info);
    }

    public Map doBuildSelectData(List<Map> typeData, Map<String, String> map) {
        Map<String, String> result = new HashMap<>();
        for (Map map1 : typeData) {
            String name = String.valueOf(map1.get("name"));
            String value = String.valueOf(map1.get("value"));
            if ("IP".equals(name)) {
                break;
            } else if (map.get(name) == null) {
                result.put("type", value);
            } else {
                result.put(name, value);
            }
        }
        return result;
    }
//    ma4.put("type", "camera_loophole");
//    ma4.put("cameraType", typeName);
/*
[{"name:"1","value":"1"},{"name:"2","value":"2"}]
 */

    public Map doBuild(List<Map> typeData, Map<String, String> mapData) {
        Map<String, Object> result = new HashMap<>();
        Map map = typeData.get(0);
        result.put("name", map.get("name"));
        mapData.put("type", String.valueOf(map.get("value")));
        List<Map> children = doBuild(typeData, 1, mapData);
        result.put("children", children);
        return result;
    }

    public List<Map> doBuild(List<Map> typeData, int n, Map<String, String> maps) {
        Map<String,String> map = new HashMap<>();
        map.putAll(maps);
        List<Map> data = combatCameraInfoMapper.selectAllByGroup(doMap(map));
        List<Map> dataList = new ArrayList<>();
        for (Map map1 : data){
            String dataName = String.valueOf(map1.get("name"));
            String type = map.get("type");
            map.put(type, dataName);
            for (int i = 0; i < typeData.size(); i++) {
                Map ma = typeData.get(i);

                if(i<n) {
                    String value = String.valueOf(ma.get("value"));
                    if(map.get(value)==null){
                        map.put(value, dataName);
                    }
                }
                if(i==n){
                    map.put("type",String.valueOf(ma.get("value")));
                }
            }

            List<Map> ppList = new ArrayList<>();
            Map<String, Object> pp = new HashMap<>();
            if(typeData.size()>(n)) {
                Map map2 = typeData.get(n);
                pp.put("name", map2.get("name"));
                List<Map> children = doBuild(typeData, n+1, map);
                pp.put("children", children);
            }else{
                pp.put("name", "IP");
                CombatCameraInfo combatCameraInfo = new CombatCameraInfo();
                doSelectCombatCameraInfoList(combatCameraInfo, map);
                List<CombatCameraInfo> combatCameraInfos = combatCameraInfoMapper.selectCombatCameraInfoList10(combatCameraInfo);
                List<Map> ipMapList = new ArrayList<>();
                for (CombatCameraInfo cameraInfo : combatCameraInfos) {
                    Map data1 = new HashMap();
                    data1.put("name", cameraInfo.getCameraIp());
                    data1.put("info", cameraInfo);
                    ipMapList.add(data1);
                }
                pp.put("children", ipMapList);
            }
            ppList.add(pp);
            map1.put("children", ppList);
            dataList.add(map1);
        }
        return dataList;
    }

    public Map doMap(Map<String,String> map){
        Map result = new HashMap();
        map.keySet().stream().forEach(key->{
            if("type".equals(key)){
                result.put("type", map.get(key));
            } else if ("camera_type".equals(key)) {
                result.put("cameraType", map.get(key));
            } else if ("camera_brand".equals(key)) {
                result.put("cameraBrand", map.get(key));
            }else if ("camera_port".equals(key)) {
                result.put("cameraPort", map.get(key));
            }else if ("camera_fra_point".equals(key)) {
                result.put("cameraFraPoint", map.get(key));
            }else if ("camera_loophole".equals(key)) {
                result.put("cameraLoophole", map.get(key));
            }
        });
        return result;
    }

    public CombatCameraInfo doSelectCombatCameraInfoList(CombatCameraInfo combatCameraInfo, Map<String, String> map) {
        if (map.get("camera_type") != null) {
            combatCameraInfo.setCameraType(String.valueOf(map.get("camera_type")));
        }
        if (map.get("camera_brand") != null) {
            combatCameraInfo.setCameraBrand(String.valueOf(map.get("camera_brand")));
        }
        if (map.get("camera_port") != null) {
            combatCameraInfo.setCameraPort(String.valueOf(map.get("camera_port")));
        }
        if (map.get("camera_fra_point") != null) {
            combatCameraInfo.setCameraFraPoint(String.valueOf(map.get("camera_fra_point")));
        }
        if (map.get("camera_loophole") != null) {
            combatCameraInfo.setCameraLoophole(String.valueOf(map.get("camera_loophole")));
        }
        return combatCameraInfo;
    }


    public Map getGraphByCameraInfoNew2(CombatCameraInfo info) {
        Map<String, Object> result = new HashMap<>();
        result.put("name", "类型");
        //查询条件
        Map<String, Object> ma = new HashMap<>();
        if (info != null) {
            if (StringUtils.isNotEmpty(info.getCameraIp())) {
                ma.put("cameraIp", info.getCameraIp());
            }
            if (StringUtils.isNotEmpty(info.getCameraArea())) {
                ma.put("cameraArea", info.getCameraArea());
            }
        }

        ma.put("type", "camera_type");
        List<Map> type = combatCameraInfoMapper.selectAllByGroup(ma);
        List<Map> typeList = new ArrayList<>();
        for (Map map : type) {
            String typeName = String.valueOf(map.get("name"));
            if (StringUtils.isNotEmpty(typeName)) {
                List<Map> ppList = new ArrayList<>();
                Map<String, Object> pp = new HashMap<>();
                pp.put("name", "品牌");
                Map<String, Object> ma1 = new HashMap<>();
                ma1.putAll(ma);
                ma1.put("type", "camera_brand");
                ma1.put("cameraType", typeName);
                List<Map> pptype = combatCameraInfoMapper.selectAllByGroup(ma1);
                List<Map> ppMapList = new ArrayList<>();
                for (Map map1 : pptype) {
                    String ppName = String.valueOf(map1.get("name"));
                    if (StringUtils.isNotEmpty(ppName)) {
                        List<Map> portList = new ArrayList<>();
                        Map<String, Object> port = new HashMap<>();
                        port.put("name", "端口");
                        Map<String, Object> ma2 = new HashMap<>();
                        ma2.putAll(ma);
                        ma2.put("type", "camera_port");
                        ma2.put("cameraType", typeName);
                        ma2.put("cameraBrand", ppName);
                        List<Map> porttype = combatCameraInfoMapper.selectAllByGroup(ma2);
                        List<Map> portMapList = new ArrayList<>();
                        for (Map map2 : porttype) {
                            String portName = String.valueOf(map2.get("name"));
                            if (StringUtils.isNotEmpty(portName)) {
                                List<Map> pointList = new ArrayList<>();
                                Map<String, Object> point = new HashMap<>();
                                point.put("name", "脆弱点");
                                Map<String, Object> ma3 = new HashMap<>();
                                ma3.putAll(ma);
                                ma3.put("type", "camera_fra_point");
                                ma3.put("cameraType", typeName);
                                ma3.put("cameraBrand", ppName);
                                ma3.put("cameraPort", portName);
                                List<Map> pointtype = combatCameraInfoMapper.selectAllByGroup(ma3);
                                List<Map> pointMapList = new ArrayList<>();
                                for (Map map3 : pointtype) {
                                    String pointName = String.valueOf(map3.get("name"));
                                    if (StringUtils.isNotEmpty(pointName)) {
                                        List<Map> loopholeList = new ArrayList<>();
                                        Map<String, Object> loophole = new HashMap<>();
                                        loophole.put("name", "漏洞类型");
                                        Map<String, Object> ma4 = new HashMap<>();
                                        ma4.putAll(ma);
                                        ma4.put("type", "camera_loophole");
                                        ma4.put("cameraType", typeName);
                                        ma4.put("cameraBrand", ppName);
                                        ma4.put("cameraPort", portName);
                                        ma4.put("cameraFraPoint", pointName);
                                        List<Map> loopholetype = combatCameraInfoMapper.selectAllByGroup(ma4);
                                        List<Map> loopholeMapList = new ArrayList<>();
                                        for (Map map4 : loopholetype) {
                                            String loopholeName = String.valueOf(map4.get("name"));
                                            if (StringUtils.isNotEmpty(loopholeName)) {
                                                List<Map> ipList = new ArrayList<>();
                                                Map<String, Object> ip = new HashMap<>();
                                                ip.put("name", "IP");

                                                CombatCameraInfo combatCameraInfo = new CombatCameraInfo();
                                                combatCameraInfo.setCameraLoophole(loopholeName);
                                                combatCameraInfo.setCameraFraPoint(pointName);
                                                combatCameraInfo.setCameraPort(portName);
                                                combatCameraInfo.setCameraBrand(ppName);
                                                combatCameraInfo.setCameraType(typeName);
                                                List<CombatCameraInfo> combatCameraInfos = combatCameraInfoMapper.selectCombatCameraInfoList(combatCameraInfo);
                                                List<Map> ipMapList = new ArrayList<>();
                                                for (CombatCameraInfo cameraInfo : combatCameraInfos) {
                                                    Map data = new HashMap();
                                                    data.put("name", cameraInfo.getCameraIp());
                                                    data.put("info", cameraInfo);
                                                    ipMapList.add(data);
                                                }
                                                ip.put("children", ipMapList);
                                                ipList.add(ip);
                                                map4.put("children", ipList);
                                                loopholeMapList.add(map4);
                                            }
                                        }
                                        loophole.put("children", loopholeMapList);
                                        loopholeList.add(loophole);
                                        map3.put("children", loopholeList);
                                        pointMapList.add(map3);
                                    }

                                }
                                point.put("children", pointMapList);
                                pointList.add(point);
                                map2.put("children", pointList);
                                portMapList.add(map2);
                            }
                        }
                        port.put("children", portMapList);
                        portList.add(port);
                        map1.put("children", portList);
                        ppMapList.add(map1);
                    }
                }
                pp.put("children", ppMapList);
                ppList.add(pp);
                map.put("children", ppList);
                typeList.add(map);
            }
        }
        result.put("children", typeList);
        return result;
    }


    public List<Map> getGraphByCameraInfoNew1(CombatCameraInfo info) {
        List<Map> resultList = new ArrayList<>();
        //类型
        Map<String, Object> typeMap1 = new HashMap<>();
        typeMap1.put("type", "camera_type");
        typeMap1.put("cameraPort", "7000");
        List<Map> type = combatCameraInfoMapper.selectAllByGroup(typeMap1);
        List<Map> typeList = new ArrayList<>();
        for (Map map : type) {
            String s = String.valueOf(map.get("name"));
            if (StringUtils.isNotEmpty(s)) {
                CombatCameraInfo combatCameraInfo = new CombatCameraInfo();
                combatCameraInfo.setCameraType(s);
                List<CombatCameraInfo> combatCameraInfos = combatCameraInfoMapper.selectCombatCameraInfoList(combatCameraInfo);
                map.put("children", combatCameraInfos);
                typeList.add(map);
            }
        }
        Map typeMap = new HashMap<>();
        typeMap.put("name", "类型");
        typeMap.put("children", typeList);
        resultList.add(typeMap);

      /*  //品牌
        List<Map> brand = combatCameraInfoMapper.selectAllByGroup("camera_brand");
        List<Map> brandList = new ArrayList<>();
        for (Map map : brand) {
            String s = String.valueOf(map.get("name"));
            if (StringUtils.isNotEmpty(s)) {
                CombatCameraInfo combatCameraInfo = new CombatCameraInfo();
                combatCameraInfo.setCameraBrand(s);
                List<CombatCameraInfo> combatCameraInfos = combatCameraInfoMapper.selectCombatCameraInfoList(combatCameraInfo);
                map.put("children", combatCameraInfos);
                brandList.add(map);
            }
        }
        Map brandMap = new HashMap<>();
        brandMap.put("name", "品牌");
        brandMap.put("children", brandList);
        resultList.add(brandMap);*/

     /*   //端口号
        List<Map> port = combatCameraInfoMapper.selectAllByGroup("camera_port");
        List<Map> portList = new ArrayList<>();
        for (Map map : port) {
            String s = String.valueOf(map.get("name"));
            if (StringUtils.isNotEmpty(s)) {
                CombatCameraInfo combatCameraInfo = new CombatCameraInfo();
                combatCameraInfo.setCameraPort(s);
                List<CombatCameraInfo> combatCameraInfos = combatCameraInfoMapper.selectCombatCameraInfoList(combatCameraInfo);
                map.put("children", combatCameraInfos);
                portList.add(map);
            }
        }
        Map portMap = new HashMap<>();
        portMap.put("name", "端口号");
        portMap.put("children", portList);
        resultList.add(portMap);*/
        /*//脆弱点
        List<Map> point = combatCameraInfoMapper.selectAllByGroup("camera_fra_point");
        List<Map> pointList = new ArrayList<>();
        for (Map map : point) {
            String s = String.valueOf(map.get("name"));
            if (StringUtils.isNotEmpty(s)) {
                CombatCameraInfo combatCameraInfo = new CombatCameraInfo();
                combatCameraInfo.setCameraPort(s);
                List<CombatCameraInfo> combatCameraInfos = combatCameraInfoMapper.selectCombatCameraInfoList(combatCameraInfo);
                map.put("children", combatCameraInfos);
                pointList.add(map);
            }
        }
        Map pointMap = new HashMap<>();
        pointMap.put("name", "端口号");
        pointMap.put("children", pointList);
        resultList.add(pointMap);
        //漏洞类型
        List<Map> loophole = combatCameraInfoMapper.selectAllByGroup("camera_loophole");
        List<Map> loopholeList = new ArrayList<>();
        for (Map map : loophole) {
            String s = String.valueOf(map.get("name"));
            if (StringUtils.isNotEmpty(s)) {
                CombatCameraInfo combatCameraInfo = new CombatCameraInfo();
                combatCameraInfo.setCameraLoophole(s);
                List<CombatCameraInfo> combatCameraInfos = combatCameraInfoMapper.selectCombatCameraInfoList(combatCameraInfo);
                map.put("children", combatCameraInfos);
                loopholeList.add(map);
            }
        }
        Map loopholeMap = new HashMap<>();
        loopholeMap.put("name", "漏洞类型");
        loopholeMap.put("children", loopholeList);
        resultList.add(loopholeMap);
        //影响范围
        List<Map> effect = combatCameraInfoMapper.selectAllByGroup("camera_effect");
        List<Map> effectList = new ArrayList<>();
        for (Map map : effect) {
            String s = String.valueOf(map.get("name"));
            if (StringUtils.isNotEmpty(s)) {
                CombatCameraInfo combatCameraInfo = new CombatCameraInfo();
                combatCameraInfo.setCameraEffect(s);
                List<CombatCameraInfo> combatCameraInfos = combatCameraInfoMapper.selectCombatCameraInfoList(combatCameraInfo);
                map.put("children", combatCameraInfos);
                effectList.add(map);
            }
        }
        Map effectMap = new HashMap<>();
        effectMap.put("name", "影响范围");
        effectMap.put("children", effectList);
        resultList.add(effectMap);
        //危害等级
        List<Map> level = combatCameraInfoMapper.selectAllByGroup("camera_level");
        List<Map> levelList = new ArrayList<>();
        for (Map map : level) {
            String s = String.valueOf(map.get("name"));
            if (StringUtils.isNotEmpty(s)) {
                CombatCameraInfo combatCameraInfo = new CombatCameraInfo();
                combatCameraInfo.setCameraEffect(s);
                List<CombatCameraInfo> combatCameraInfos = combatCameraInfoMapper.selectCombatCameraInfoList(combatCameraInfo);
                map.put("children", combatCameraInfos);
                levelList.add(map);
            }
        }
        Map levelMap = new HashMap<>();
        levelMap.put("name", "危害等级");
        levelMap.put("children", levelList);
        resultList.add(levelMap);
        //利用方法
        List<Map> method = combatCameraInfoMapper.selectAllByGroup("camera_method");
        List<Map> methodList = new ArrayList<>();
        for (Map map : method) {
            String s = String.valueOf(map.get("name"));
            if (StringUtils.isNotEmpty(s)) {
                CombatCameraInfo combatCameraInfo = new CombatCameraInfo();
                combatCameraInfo.setCameraMethod(s);
                List<CombatCameraInfo> combatCameraInfos = combatCameraInfoMapper.selectCombatCameraInfoList(combatCameraInfo);
                map.put("children", combatCameraInfos);
                methodList.add(map);
            }
        }
        Map methodMap = new HashMap<>();
        methodMap.put("name", "危害等级");
        methodMap.put("children", methodList);
        resultList.add(methodMap);*/


        return resultList;
    }

    @Override
    public List<Map> getloopholeType() {
        return combatCameraInfoMapper.getloopholeType();
    }

    @Override
    public List<Map> getCameraArea() {
        List<Map> cameraArea = combatCameraInfoMapper.getCameraArea();
//        cameraArea.sort((map1, map2) -> ((Long) map2.get("count")).compareTo((Long) map1.get("count")));
        cameraArea.sort((map1, map2) -> ((Long) map2.get("count")).compareTo((Long) map1.get("count")));
        cameraArea = cameraArea.subList(0, 10);
        Collections.reverse(cameraArea);

        return cameraArea;
    }

    @Override
    public Map getCameraNum() {
        Map re = new HashMap();
        Set<String> set = new HashSet<>();
        List<CombatCameraInfo> list = combatCameraInfoMapper.selectCombatCameraInfoList(null);
        for (CombatCameraInfo cameraInfo : list) {
            String cameraBrand = cameraInfo.getCameraBrand();
            set.add(cameraBrand);
        }
        re.put("cameraNum", list.size());
        re.put("cameraBrandNum", set.size());
        return re;
    }

    @Override
    public List<CombatCameraGeoResult> getCameraAddress(Long id) {
        CombatCameraGeo combatCameraGeo = new CombatCameraGeo();
        combatCameraGeo.setCameraId(id);
        List<CombatCameraGeo> combatCameraGeos = combatCameraGeoMapper.selectCombatCameraGeoList(combatCameraGeo);
        List<CombatCameraGeoResult> list = new ArrayList<>();
        for (CombatCameraGeo geo : combatCameraGeos) {
            Long id1 = geo.getId();
            CombatCameraGeoResult combatCameraGeoResult = new CombatCameraGeoResult();
            combatCameraGeoResult.setGeoId(id1);
            List<CombatCameraGeoResult> combatCameraGeoResults = combatCameraGeoResultMapper.selectCombatCameraGeoResultList(combatCameraGeoResult);
            list.addAll(combatCameraGeoResults);
        }
        return list;
    }

    @Override
    public String importCombatCameraInfo(List<CombatCameraInfo> list, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (CombatCameraInfo user : list) {
            try {
                CombatCameraInfo in = new CombatCameraInfo();
                in.setCameraIp(user.getCameraIp());
                in.setCameraPort(user.getCameraPort());
                // 验证是否存在这个用户
//                List<CombatCameraInfo> u = combatCameraInfoMapper.selectCombatCameraInfoList(in);
                List<CombatCameraInfo> u = new ArrayList<>();

                if (u.size() <= 0) {
                    user.setCreateBy(operName);
                    combatCameraInfoMapper.insertCombatCameraInfo(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、摄像头 " + user.getCameraIp() + ":" + user.getCameraPort() + " 导入成功");
                } else if (isUpdateSupport) {
                    CombatCameraInfo combatCameraInfo = u.get(0);
                    user.setId(combatCameraInfo.getId());
                    combatCameraInfoMapper.updateCombatCameraInfo(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、摄像头 " + user.getCameraIp() + ":" + user.getCameraPort() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、摄像头 " + user.getCameraIp() + ":" + user.getCameraPort() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、摄像头 " + user.getCameraIp() + ":" + user.getCameraPort() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<Map> getCameraBrand() {
        List<Map> maps = combatCameraInfoMapper.brandList();
        return maps;
    }

    private Map<String, String> getGraph(String userid, String targetid, String relation, String targetName, String plate) {
        Map<String, String> graphMap = new HashMap<>();
        graphMap.put("userid", userid);
        graphMap.put("targetid", targetid);
        graphMap.put("relation", relation);
        graphMap.put("targetName", targetName);
        if (StringUtils.isNotEmpty(plate))
            graphMap.put("plate", plate);
        return graphMap;
    }

    public List<Category> getCategory() {
        List<Category> categories = new ArrayList<>();
        categories.add(new Category("摄像头ip"));
        categories.add(new Category("类型"));
        categories.add(new Category("品牌"));
        categories.add(new Category("端口号"));
        categories.add(new Category("脆弱点"));
        categories.add(new Category("漏洞类型"));
        categories.add(new Category("影响范围"));
        categories.add(new Category("危害等级"));
        categories.add(new Category("利用方法"));
        return categories;
    }

    public String StringtoMD5(String content) {
        try {
            // 1. 创建MessageDigest实例，并指定"MD5"为算法参数
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 2. 更新MessageDigest实例，输入需要转换为MD5的原始数据
            byte[] inputBytes = content.getBytes();
            md.update(inputBytes);

            // 3. 完成MessageDigest实例的更新后，调用digest方法计算MD5值
            byte[] hashBytes = md.digest();

            // 4. 将MD5值转换为16进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : hashBytes) {
                sb.append(String.format("%02x", b));
            }
            String md5Hash = sb.toString();

            // 输出MD5哈希值
            return md5Hash;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return content;
    }
}
